<template>
  <div class="team-detail">
    <div class="header">
      <span class="back" @click="goBack">&lt; 返回</span>
      <span class="star"></span>
    </div>
    <div v-if="team">
      <div class="title-bar">
        <span class="team-title">{{ team.name }}</span>
        <span class="team-count">成员：{{ team.members }}人</span>
      </div>
      <div class="section-title">班组介绍</div>
      <div class="desc">{{ team.intro }}</div>
      <div class="section-title">班组成员</div>
      <div class="members">
        <div class="member-card" v-for="m in team.memberList" :key="m.id">
          <img :src="m.avatar" class="avatar" />
          <div class="info">
            <div class="name">{{ m.name }}</div>
            <div class="role">{{ m.role }}</div>
            <div class="tag" v-if="m.leader">组长</div>
          </div>
        </div>
      </div>
      <button class="stat-btn" @click="showTeamStats">任务完成统计</button>
      <div class="footer-bar">
        <button v-if="!team.joined" class="join-btn" @click="joinTeam">申请加入班组</button>
        <button v-else class="joined-btn" disabled>已加入</button>
      </div>
    </div>
    <div v-else>
      <p>未找到该班组信息</p>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTeamStore } from '../stores/TeamStatsStore.js'

const router = useRouter()
const route = useRoute()
const teamStore = useTeamStore()

const team = computed(() => teamStore.teams.find(t => t.id === parseInt(route.params.id)))

function goBack() {
  router.back()
}

function showTeamStats() {
  router.push(`/team-stats/${route.params.id}`)
}

function joinTeam() {
  if (team.value && !team.value.joined) {
    teamStore.joinTeam(team.value.id)
    // 可以添加一些用户反馈，比如显示成功消息
    alert('申请加入班组成功！')
  }
}
</script>

<style scoped>
.team-detail {
  background: #f7f8fa;
  min-height: 100vh;
  max-width: 375px;
  margin: 0 auto;
  box-sizing: border-box;
  padding-bottom: 80px;
}
.header {
  display: flex;
  align-items: center;
  height: 48px;
  background: #fff;
  font-size: 16px;
  font-weight: bold;
  border-bottom: 1px solid #f0f0f0;
  padding: 0 16px;
  justify-content: space-between;
}
.back {
  color: #333;
  cursor: pointer;
}
.star {
  width: 22px;
  height: 22px;
  display: inline-block;
  background: url('data:image/svg+xml;utf8,<svg width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M11 2.5l2.472 6.13 6.528.47-5.1 4.47 1.6 6.43L11 16.1l-5.5 3.9 1.6-6.43-5.1-4.47 6.528-.47L11 2.5z" stroke="%23FFD700" stroke-width="1.5" fill="none"/></svg>') center/contain no-repeat;
}
.title-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 18px 16px 0 16px;
}
.team-title {
  font-size: 18px;
  font-weight: bold;
  color: #222;
  flex: 1;
  line-height: 1.3;
}
.team-count {
  color: #888;
  font-size: 14px;
  margin-left: 8px;
}
.section-title {
  font-size: 15px;
  font-weight: bold;
  color: #222;
  margin: 18px 0 6px 16px;
}
.desc {
  color: #444;
  font-size: 14px;
  margin: 0 16px 0 16px;
  line-height: 1.7;
}
.members {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 10px;
  margin: 10px 16px 0 16px;
}
.member-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px #f0f1f2;
  width: calc(50% - 5px);
  display: flex;
  align-items: center;
  padding: 10px 8px;
  margin-bottom: 0;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-right: 10px;
}
.info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.name {
  font-weight: bold;
  font-size: 15px;
  color: #222;
}
.role {
  color: #888;
  font-size: 13px;
}
.tag {
  color: #409eff;
  font-size: 12px;
  margin-top: 2px;
}
.stat-btn {
  display: block;
  width: 180px;
  margin: 24px auto 0 auto;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 10px 0;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
}
.footer-bar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  background: #fff;
  box-shadow: 0 -1px 4px #eee;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  max-width: 375px;
  width: 100vw;
  margin: 0 auto;
}
.join-btn {
  width: 90%;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 14px 0;
  font-size: 17px;
  font-weight: 500;
  cursor: pointer;
}
.joined-btn {
  width: 90%;
  background: #888;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 14px 0;
  font-size: 17px;
  font-weight: 500;
  cursor: not-allowed;
}
</style> 